<template>
    <div id ="my-element" style="height: 100vh;overflow: hidden;display: flex;justify-content: center;align-items: center;background-color: #0f9876">
        <div style="display: flex;background-color: white;opacity:0.94;width: 800px;height: 400px;border-radius: 10px">

            <div style="flex : 1;padding: 50px;opacity:0.95;">
                <img src="@/assets/logo1.jpg" alt="" style="width: 300px;height: 300px;">
            </div>

            <div style="flex : 1;width: 300px;height: 290px;padding-top: 50px;display: flex;align-items: center;padding-left: 20px;margin-top: 30px">
                <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
                    <div style="font-size: 23px; font-weight: bold; text-align: center; margin-bottom: 20px">注册</div>
                    <el-form-item prop="username">
                        <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
                    </el-form-item>
                    <el-form-item prop="confirmPass">
                        <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
                    </el-form-item>
<!--                    <el-form-item prop="role">-->
<!--                        <el-radio-group v-model="user.role">-->
<!--                            <el-radio label="用户"></el-radio>-->
<!--                            <el-radio label="商家"></el-radio>-->
<!--                        </el-radio-group>-->
<!--                    </el-form-item>-->
                    <el-form-item>
                        <el-button  plain type="warning" style="width: 100%" @click="register">注 册</el-button>
                    </el-form-item>
                    <div style="display: flex">
                        <div style="flex: 1 ;color: grey;font-size: 15px">已经注册？<span style="color: #d99838; cursor: pointer" @click="$router.push('/login')">登录</span></div>
                    </div>
                </el-form>
            </div>
        </div>

    </div>
</template>

<script>

    export default {
        name: "Register",
        data() {
            // 验证码校验
            const validatePassword = (rule, confirmPass, callback) => {
                if (confirmPass === '') {
                    callback(new Error('请确认密码'))
                } else if (confirmPass !== this.user.password) {
                    callback(new Error('两次输入的密码不一致'))
                } else {
                    callback()
                }
            }
            return {
                user: {
                    username: '',
                    password: '',
                    confirmPass: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                    confirmPass: [
                        { validator: validatePassword, trigger: 'blur' }
                    ],
                    // role: [
                    //     { required: true, message: '请选择角色', trigger: 'blur' },
                    // ],
                }
            }
        },
        created() {

        },
        methods: {
            register() {
                this.$refs['registerRef'].validate((valid) => {
                    if (valid) {
                        // 验证通过
                        this.$request.post('/register', this.user).then(res => {
                            if (res.code === '200') {
                                this.$router.push('/login')
                                this.$message.success('注册成功')
                            } else {
                                this.$message.error('注册失败')
                            }
                        })
                    }
                })
            }
        }
    }
</script>

<style scoped>
    #my-element {
        background: url('../assets/背景31.jpg'); /* 注意相对路径 */
        background-repeat: no-repeat;
        background-position: center;
        background-size: cover; /* 根据需求选择合适的值，比如cover、contain等 */
    }
</style>
